package com.wdhcr.service.imp;

import com.wdhcr.entity.Notice;
import com.wdhcr.repository.NoticeRepository;
import com.wdhcr.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeServiceImp implements NoticeService {

    @Autowired
    private NoticeRepository noticeRepository;
    @Override
    public Notice saveNotice(Notice notice) {
        return noticeRepository.save(notice);
    }

    @Override
    public Notice getNotice(Long id) {
        return noticeRepository.findById(id).get();
    }

    @Override
    public List<Notice> listNotice() {
        return null;
    }


    @Override
    public List<Notice> listNoticeTop(Integer size) {
        //根据更新时间倒叙排列
        Sort sort = Sort.by(Sort.Direction.DESC,"weight");
        PageRequest pageRequest = PageRequest.of(0, size,sort);
        return noticeRepository.findAll(pageRequest).toList();
    }

    @Override
    public Page<Notice> listNotice(Pageable pageable) {
        return noticeRepository.findAll(pageable);
    }


    @Override
    public void deleteById(Long id) {
        noticeRepository.deleteById(id);
    }
}
